package com.axend.aerosense.common.adapter;

import android.graphics.Typeface;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.axend.aerosense.common.R;
import com.axend.aerosense.common.bean.HomeReportBean;
import com.axend.aerosense.common.databinding.CommonUserListItemBinding;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;

public class CommonUserListAdapter
        extends BaseQuickAdapter<HomeReportBean, BaseDataBindingHolder<CommonUserListItemBinding>> {

    //默认选中第一个
    private int selectItem = 0;

    public CommonUserListAdapter() {
        super(R.layout.common_user_list_item);
    }

    @Override
    protected void convert(@NonNull BaseDataBindingHolder<CommonUserListItemBinding> holder,
                           HomeReportBean bean) {

        CommonUserListItemBinding binding = holder.getDataBinding();
        binding.commonTvUserName.setText(bean.getWardName());
        binding.commonTvUserName.setTextColor(
                getContext().getColor(isSelected(bean) ? R.color.app_colorAccent : R.color.base_text_color_black)
        );

        if (isSelected(bean)) {
            binding.commonTvUserName.getPaint().setFakeBoldText(isSelected(bean));
        } else {
            binding.commonTvUserName.setTypeface(
                    Typeface.create(binding.commonTvUserName.getTypeface(), Typeface.NORMAL),
                    Typeface.NORMAL);
        }
        binding.commonTvUserName.invalidate();

        binding.commonIvUserHeart.setBackgroundResource(isSelected(bean) ? R.drawable.common_shape_heard_border : 0);

        binding.commonIvUserSelect.setVisibility(isSelected(bean) ? View.VISIBLE : View.GONE);
        binding.commonIvArrow.setVisibility(isSelected(bean) ? View.GONE : View.VISIBLE );
        loadHeadImage(binding.commonIvUserHeart, bean.getWardImg());

        binding.commonViewUnderline.setVisibility(getItemPosition(bean) == getItemCount() - 1 ? View.GONE : View.VISIBLE);
    }

    /** 加载用户头像图片 */
    private void loadHeadImage(ImageView view, String url){
        CommonBindingAdapters.imageCircleHeadUrl(view, url);
    }

    /** 是否选中item */
    private boolean isSelected(HomeReportBean bean){
        int position = getItemPosition(bean);
        return selectItem == position;
    }

    /** 设置选中项 */
    public void setSelectItem(int index){
        this.selectItem = index;
        notifyDataSetChanged();
    }

    public int getSelectItem() {
        return selectItem;
    }
}
